package hit.edu.zjc.Operator.Entity;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;

/*
 * 用于计算数据条目相似度
 * */
public class DataIDSimilarityPair {
	
	//DataID,Pair(1gram权重，2gram权重)
	private Hashtable<Integer,DoublePair> PairMap=new Hashtable<Integer,DoublePair>();

	private class DoublePair
	{
		public double OneGramWeight=0;
		public double TwoGramWeight=0;
		public double weight=0;
		
		public void refresh(int rate)
		{
			double w=TwoGramWeight>OneGramWeight?TwoGramWeight:OneGramWeight;
			w=w*rate;
			weight=weight+w;
			OneGramWeight=0;
			TwoGramWeight=0;
		}
		
		public DoublePair()
		{
			OneGramWeight=0;
			TwoGramWeight=0;
			weight=0;
		}
		public DoublePair(double one,double two)
		{
			OneGramWeight=one;
			TwoGramWeight=two;
			weight=0;
		}
	}
	
	public DataIDSimilarityPair()
	{}
	
	public void refresh(int rate)
	{
		Enumeration<Integer> e=PairMap.keys();
      while(e.hasMoreElements())//遍历所有的key
        {
			Integer key=e.nextElement();
			DoublePair pair=PairMap.get(key);
			pair.refresh(rate);
			PairMap.put(key, pair);
		}
	}
	
	
	public void addOneGramWeight(int key,double weight)
	{
		DoublePair pair=PairMap.get(key);
		if(pair==null)
		{
			pair=new DoublePair();
		}
		pair.OneGramWeight=pair.OneGramWeight+weight;
		PairMap.put(key, pair);
	}
	
	public void addTwoGramWeight(int key,double weight)
	{
		DoublePair pair=PairMap.get(key);
		if(pair==null)
		{
			pair=new DoublePair();
		}
		pair.TwoGramWeight+=weight;
		PairMap.put(key, pair);
	}
	
	public HashSet<Integer> getKeySet(int threshold)
	{
		HashSet<Integer> set=new HashSet<Integer>();
		Enumeration<Integer> e=PairMap.keys();
      while(e.hasMoreElements())//遍历所有的key
        {
			Integer key=e.nextElement();
			DoublePair pair=PairMap.get(key);
			if(pair.weight>=threshold)
				set.add(key);
		}
      return set;
	}
	
	public HashSet<Integer> getKeySet(double threshold)
	{
		HashSet<Integer> set=new HashSet<Integer>();
		Enumeration<Integer> e=PairMap.keys();
      while(e.hasMoreElements())//遍历所有的key
        {
			Integer key=e.nextElement();
			DoublePair pair=PairMap.get(key);
			if(pair.weight>=threshold)
				set.add(key);
		}
      return set;
	}

}
